import router from './router'

import NProgress from 'nprogress' // Progress 进度条
import 'nprogress/nprogress.css'// Progress 进度条样式
import { getToken } from '@/utils/auth' // 验权
import { isiOS } from '@/utils/support' // 验权

const whiteList = ['/login', '/regist', '/autocode'] // 不重定向白名单
router.beforeEach((to, from, next) => {

  let tockenstr = getToken()
  NProgress.start()

  if (tockenstr) {
    if (to.path === '/login') {
      next({ path: '/' })
      NProgress.done() 
    } else {
      next()
    }
  } else {

    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      next('/login')
      // try {
      //   window.webkit.messageHandlers.getToken.postMessage(to.path);
      //   NProgress.done()
      //   return true;
      // }
      // catch (err) {
      //   next('/login')
      // }

    }
  }
  if (to.meta.title) {
    document.title = to.meta.title
  }
})

router.afterEach(() => {
  NProgress.done() // 结束Progress
})


